package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类品牌中间表 业务实现类
 *
 * @author lhw
 * @since 2022-12-25
 */
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;
    //根据分类id查询品牌列表
    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //1 根据三级分类id查询分类品牌列表
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarks = super.list(queryWrapper);

        //2 从分类品牌列表中获取品牌id集合
        //方法一：
        /*List<Long> idList = new ArrayList<>();
        //遍历集合取出品牌id 并添加到idList中
        if (!CollectionUtils.isEmpty(baseCategoryTrademarks)){
            for (BaseCategoryTrademark baseCategoryTrademark : baseCategoryTrademarks) {
                idList.add(baseCategoryTrademark.getTrademarkId());
            }
        }*/
        //方法二：
        /*if (!CollectionUtils.isEmpty(baseCategoryTrademarks)) {
            //采用stream流处理集合 将集合对象中 品牌id 获取 并转为 品牌id集合
            List<Long> trademarkIdList = baseCategoryTrademarks.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
        }*/
        //方法三：简写方法二
        if (!CollectionUtils.isEmpty(baseCategoryTrademarks)) {
            //采用stream流处理集合 将集合对象中 品牌id 获取 并转为 品牌id集合
            List<Long> trademarkIdList = baseCategoryTrademarks.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
        //3 通过品牌id集合查询品牌列表
            List<BaseTrademark> list = baseTrademarkMapper.selectBatchIds(trademarkIdList);
            return list;
        }
        return null;
    }

    //根据分类id获取可选的品牌列表
    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        List<BaseTrademark> list = baseTrademarkMapper.findCurrentTrademarkList(category3Id);
        return list;
    }

    //将品牌关联到分类
    @Override
    public void saveBaseCategoryTrademark(CategoryTrademarkVo categoryTrademarkVo) {
        //获取品牌id集合
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        //遍历品牌id集合 构建品牌分类中间对象 执行新增
        if (!CollectionUtils.isEmpty(trademarkIdList)) {
            //用stream流将集合泛型 Long型 转为 BaseCategoryTrademark
            List<BaseCategoryTrademark> baseCategoryTrademarks = trademarkIdList.stream().map(trademarkId -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setTrademarkId(trademarkId);
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                return baseCategoryTrademark;
            }).collect(Collectors.toList());
            //执行批量保存方法
            super.saveBatch(baseCategoryTrademarks);
        }

    }

    //删除分类品牌关联
    @Override
    public void removeCategoryTrademark(Long category3Id, Long trademarkId) {
        //逻辑删除
        /*LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        queryWrapper.eq(BaseCategoryTrademark::getTrademarkId,trademarkId);
        super.remove(queryWrapper);*/

        //物理删除
        baseMapper.removeCategoryTrademark(category3Id,trademarkId);
    }
}
